package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.finance.domain.Account;
import com.ruoyi.finance.domain.CzHistory;
import com.ruoyi.finance.domain.Merchant;
import com.ruoyi.finance.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import static com.ruoyi.common.utils.uuid.UUID.randomUUID;

@RestController
@RequestMapping("/finance/merchant")
public class MerchantController extends BaseController {
    @Autowired
    private IMerchantService merchantService;


    /**
     * 获取商户信息详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        Merchant merchant = new Merchant();
        merchant.setUserId(userId);
        return success(merchantService.selectMerchantByUserOne(merchant));
    }

    /**
     * 获取商户信息详细信息
     */
    @Anonymous
    @GetMapping(value = "/excludeSk/{userId}")
    public AjaxResult getInfoExcludeSk(@PathVariable("userId") Long userId) {
        Merchant merchant = new Merchant();
        merchant.setUserId(userId);
        Merchant one = merchantService.selectMerchantByUserOne(merchant);
        one.setSecretKey(null);
        return success(one);
    }

    /**
     * 修改商户信息
     */
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant) {
        // 刷新密钥
        merchant.setSecretKey(randomUUID(true).toString(true).substring(0, 16));
        return toAjax(merchantService.updateMerchant(merchant));
    }

    @Log(title = "修改商户信息", businessType = BusinessType.UPDATE)
    @GetMapping("/updata")
    public AjaxResult upData(Merchant merchant) {
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 查询商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('finance:merchant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Merchant merchant) {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 导出商户信息列表
     */
    @PreAuthorize("@ss.hasPermi('finance:merchant:export')")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant) {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商户信息数据");
    }

    /**
     * 新增商户信息
     */
    @PreAuthorize("@ss.hasPermi('finance:merchant:add')")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Merchant merchant) {
        return toAjax(merchantService.insertMerchant(merchant));
    }

    @GetMapping("/getAccounts")
    public TableDataInfo selectAccount(Account account) {
        startPage();
        List<Account> list = merchantService.selectAccountsList(account);
        return getDataTable(list);
    }

    @GetMapping("/addMerchantYue")
    public AjaxResult addMerchantYue(CzHistory cz) {
        int addBalance = merchantService.addBalanceByDuigong(cz);
        if (addBalance != 2) {
            return warn("失败！");
        }
        return toAjax(addBalance);
    }

}
